<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>addGoods.jsp</title>
<link href="../../css/admin/common.css" type="text/css" rel="stylesheet">
</head>    
<body>
	<form id="form" action="/management/product/${operation}" method="post">
        <c:if test="${product!=null}">
            <input
                type="number"
                name="id"
                value="<c:out value="${product.id}" />"
                hidden="hidden"
            >
        </c:if>
        <c:if test="${product==null}">
            <input
                type="number"
                name="id"
                value="0"
                hidden="hidden"
            >
        </c:if>
		<table border=1 style="border-collapse: collapse">
			<caption>
				<font size=4 face=华文新魏>添加商品</font>
			</caption>
			<tr>
				<td>名称<font color="red">*</font></td>
				<td>
					<input type="text" name="name" id="name" value="${product.gname}"/>
				</td>
			</tr>
			<tr>
				<td>原价<font color="red">*</font></td>
				<td>
					<input type="number" name="oldPrice" id="oldPrice" value="${product.goprice}"/>
				</td>
			</tr>
			<tr>
				<td>折扣价</td>
				<td>
					<input type="number" name="price" id="price" value="${product.grprice}"/>
				</td>
			</tr>
			<tr>
				<td>库存</td>
				<td>
					<input type="number" name="store" id="store" value="${product.gstore}"/>
				</td>
			</tr>
			<tr>
				<td>图片</td>
				<td>
					<input type="text" name="img" id="img" value="${product.gpicture}"/>
				</td>
			</tr>
			<tr>
				<td>类型</td>
				<td>
					<select name="catId" id="catId"/>
						<c:forEach items="${categories}" var="category">
                            <c:if test="${category.id==product.goodstype_id}">
                                <option value="${category.id}" selected="selected">
                                    ${category.typenameme}
                                </option>
                            </c:if>
                            <c:if test="${category.id!=product.goodstype_id}">
                                <option value="${category.id}">
                                    ${category.typenameme}
                                </option>
                            </c:if>
                        </c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td align="center">
					<input type="submit" value="提交"/>
				</td>
				<td align="left">
					<input type="reset" value="重置"/>
				</td>
			</tr>
		</table>
	</form>
</body>
<script>
    function handleSubmit() {
        var status = true;
        var alertInfo = '';
        var name = document.querySelector("#name");
        var oldPrice = document.querySelector("#oldPrice");
        var price = document.querySelector("#price");
        var store = document.querySelector("#store");

        if (name.value === '') {
            status = false;
            alertInfo += '请输入商品名称！\n';
        }
        if (oldPrice.value === '') {
            status = false;
            alertInfo += '请输入原价！\n';
        }

        if (price.value === '') {
            price.value = oldPrice.value;
        }
        if (store.value === '') {
            store.value = 0;
        }

        if (status) {
            return true;
        } else {
            alert(alertInfo);

            return false;
        }
    }

    document.querySelector("#form").onsubmit = handleSubmit;
</script>
</html>
